﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.ESS.BLL;
using KPIS.GERP.ESS.MODEL;
using KPIS.GERP.GFMS.APP.App_UI.Commons;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;


namespace KPIS.GERP.GFMS.APP.App_UI.UserControls
{
    public partial class Mail_Request : System.Web.UI.UserControl
    {
        Main main = new Main();
        B_MessageSend send = new B_MessageSend();
        B_MessageReply reply = new B_MessageReply();
        IBLL_FIM_MoneyRequests mr = new B_FIM_MoneyRequests();
        IBLL_FIM_MoneyRequestDtls mrd = new B_FIM_MoneyRequestDtls();
        IBLL_HRM_PS_Employees emp = new B_HRM_PS_Employees();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void LoadData(string strSendSeq, M_FIM_MoneyRequests info, bool bEdit)
        {
            M_HRM_PS_Employees EmpInfo = emp.GetInfo(info.emp_seq.ToString());
            lblSendSeq.Text = strSendSeq;
            lblMoneyReqSeq.Text = info.money_req_seq.ToString();
            lblEmpSeq.Text = info.emp_seq.ToString();
            lblSendDate.Text = "เมื่อวันที่   " + info.money_req_date.Value.ToShortDateString();
            lblEmpName.Text = info.emp_name;
            lblPosName.Text = EmpInfo.pos_name;
            lblPosRefNo.Text = EmpInfo.pos_ref;
            lblWorkingJobsiteName.Text = EmpInfo.working_job_site_name;
            lblMoneyReqName.Text = info.money_req_name;
            lblBgmSourceName.Text = info.bgm_source_name;

            RadGrid1.DataSource = mrd.GetFull(info.money_req_seq.ToString());
            RadGrid1.DataBind();

            double dSumBathAmount = 0;
            for (int i = 0; i < RadGrid1.Items.Count; i++)
            {
                dSumBathAmount = dSumBathAmount + Convert.ToDouble(RadGrid1.Items[i]["amount"].Text);
            }

            lblSumBathAmount.Text = dSumBathAmount.ToString("#,##0.00");
            lblTextAmount.Text = main.MoneyTextThai(dSumBathAmount);

            if (!bEdit)
            {
                UpdateSendMessageStatus(strSendSeq, "1");
            }

            cmdReply.Visible = !bEdit;
            cmdEdit.Visible = bEdit;
            cmdEditReply.Visible = false;
            rblReplyType.Enabled = !bEdit;
        }

        protected void rblReplyType_SelectedIndexChanged(object sender, EventArgs e)
        {
            PanelForword.Visible = rblReplyType.SelectedValue == "2";
            PanelReply.Visible = rblReplyType.SelectedValue == "0";
        }

        protected void cmdReply_Click(object sender, EventArgs e)
        {
            string strUserID = Session["EmpSeq"].ToString();
            string strReplyText = null;

            switch (rblReplyType.SelectedValue)
            {
                case "0" :
                    strReplyText = txtReplyText.Text;
                    break;
                case "1" :
                    strReplyText = "เห็นชอบให้ยืม";
                    break;
                case "2" :
                    strReplyText = "เห็นชอบให้ยืม และรอการพิจารณาในขั้นต่อไป";

                    InsertForword();
                    break;
            }

            //================ insert message_reply ====================
            M_MessageReply ReplyInfo = new M_MessageReply();
            ReplyInfo.reply_date = DateTime.Today;
            ReplyInfo.ref_no = null;
            ReplyInfo.reply_text = strReplyText;
            ReplyInfo.from_emp_seq = main.ConvertNullInt(strUserID);
            ReplyInfo.from_pos_seq = send.GetPosSeqFromEmpSeq(strUserID);
            ReplyInfo.to_emp_seq = main.ConvertNullInt(lblEmpSeq.Text);
            ReplyInfo.to_pos_seq = send.GetPosSeqFromEmpSeq(lblEmpSeq.Text);
            ReplyInfo.from_send_seq = main.ConvertNullInt(lblSendSeq.Text);
            ReplyInfo.remarks = null;
            ReplyInfo.reply_type = rblReplyType.SelectedValue;
            ReplyInfo.message_status = "0";
            ReplyInfo.created_by = main.ConvertNullInt(strUserID);
            ReplyInfo.created_when = DateTime.Now;

            reply.Insert(ReplyInfo);

            //================ update allow =====================
            UpdateBorrowAllow(lblMoneyReqSeq.Text, rblReplyType.SelectedValue);

            //================ update message_send =====================
            UpdateSendMessageStatus(lblSendSeq.Text, "2");

            ((Forms.TaskLists)this.Parent.Page).ViewRequest();
        }

        protected void cmdEditReply_Click(object sender, EventArgs e)
        {
            string strUserID = Session["EmpSeq"].ToString();
            string strSendSeq = lblSendSeq.Text;
            string strForwordSendSeq = null;
            string strReplyText = null;

            switch (rblReplyType.SelectedValue)
            {
                case "0":
                    strReplyText = txtReplyText.Text;
                    strForwordSendSeq = CheckIsForword();

                    if (strForwordSendSeq.Length > 0)
                    {
                        M_MessageSend info = new M_MessageSend();
                        info.send_seq = Convert.ToInt32(strForwordSendSeq);
                        info.message_status = "0";
                        info.updated_by = main.ConvertNullInt(strUserID);
                        info.updated_when = DateTime.Now;
                        send.UpdateMessageStatus(info);
                    }
                    break;
                case "1":
                    strReplyText = "เห็นชอบให้ยืม";
                    strForwordSendSeq = CheckIsForword();

                    if (strForwordSendSeq.Length > 0)
                    {
                        M_MessageSend info = new M_MessageSend();
                        info.send_seq = Convert.ToInt32(strForwordSendSeq);
                        info.message_status = "1";
                        info.updated_by = main.ConvertNullInt(strUserID);
                        info.updated_when = DateTime.Now;
                        send.UpdateMessageStatus(info);
                    }
                    break;
                case "2":
                    strReplyText = "เห็นชอบให้ยืม และรอการพิจารณาในขั้นต่อไป";
                    strForwordSendSeq = CheckIsForword();

                    if (strForwordSendSeq.Length > 0)
                    {
                        M_MessageSend info = new M_MessageSend();
                        info.send_seq = Convert.ToInt32(strForwordSendSeq);
                        info.message_status = "2";
                        info.updated_by = main.ConvertNullInt(strUserID);
                        info.updated_when = DateTime.Now;
                        send.UpdateMessageStatus(info);

                        UpdateForword(strForwordSendSeq);
                    }
                    else
                    {
                        InsertForword();
                    }
                    break;
            }

            //================ update message_reply ====================
            M_MessageReply ReplyInfo = reply.GetInfoFromSendSeq(strSendSeq);
            //ReplyInfo.reply_date = DateTime.Today;
            //ReplyInfo.ref_no = null;
            ReplyInfo.reply_text = strReplyText;
            //ReplyInfo.from_emp_seq = main.ConvertNullInt(strUserID);
            ReplyInfo.to_emp_seq = main.ConvertNullInt(lblEmpSeq.Text);
            ReplyInfo.to_pos_seq = send.GetPosSeqFromEmpSeq(lblEmpSeq.Text);
            //ReplyInfo.from_send_seq = main.ConvertNullInt(lblSendSeq.Text);
            ReplyInfo.remarks = null;
            ReplyInfo.reply_type = rblReplyType.SelectedValue;
            //ReplyInfo.message_status = "0";
            ReplyInfo.updated_by = main.ConvertNullInt(strUserID);
            ReplyInfo.updated_when = DateTime.Now;

            reply.Update(ReplyInfo);

            //================ update allow =====================
            UpdateBorrowAllow(lblMoneyReqSeq.Text, rblReplyType.SelectedValue);

            //================ update message_send =====================
            UpdateSendMessageStatus(strSendSeq, "2");

            ((Forms.TaskLists)this.Parent.Page).ViewRequest();
        }

        protected void cmdCancel_Click(object sender, EventArgs e)
        {
            ((Forms.TaskLists)this.Parent.Page).ViewRequest();
        }

        protected void InsertForword()
        {
            string strUserID = Session["EmpSeq"].ToString();
            M_MessageSend sendInfo = new M_MessageSend();

            sendInfo.send_date = DateTime.Today;
            sendInfo.ref_no = null;
            sendInfo.send_text = "";
            sendInfo.ref_type = "fim_money_requests";
            sendInfo.ref_seq = main.ConvertNullInt(lblMoneyReqSeq.Text);
            sendInfo.from_emp_seq = main.ConvertNullInt(strUserID);
            sendInfo.from_pos_seq = send.GetPosSeqFromEmpSeq(strUserID);
            sendInfo.to_emp_seq = main.ConvertNullInt(ddlEmpSeq.SelectedValue);
            sendInfo.to_pos_seq = send.GetPosSeqFromEmpSeq(ddlEmpSeq.SelectedValue);
            sendInfo.from_send_seq = main.ConvertNullInt(lblSendSeq.Text);
            sendInfo.remarks = null;
            sendInfo.message_status = "0";
            sendInfo.created_by = main.ConvertNullInt(strUserID);
            sendInfo.created_when = DateTime.Now;

            send.Insert(sendInfo);
        }

        protected void UpdateForword(string strForwordSendSeq)
        {
            string strUserID = Session["EmpSeq"].ToString();
            M_MessageSend sendInfo = new M_MessageSend();

            //sendInfo.send_date = DateTime.Today;
            sendInfo.send_seq = Convert.ToInt32(strForwordSendSeq);
            sendInfo.ref_no = null;
            sendInfo.send_text = "";
            sendInfo.ref_seq = main.ConvertNullInt(lblMoneyReqSeq.Text);
            sendInfo.from_emp_seq = main.ConvertNullInt(strUserID);
            sendInfo.from_pos_seq = send.GetPosSeqFromEmpSeq(strUserID);
            sendInfo.to_emp_seq = main.ConvertNullInt(ddlEmpSeq.SelectedValue);
            sendInfo.to_pos_seq = send.GetPosSeqFromEmpSeq(ddlEmpSeq.SelectedValue);
            sendInfo.from_send_seq = main.ConvertNullInt(lblSendSeq.Text);
            sendInfo.remarks = null;
            sendInfo.message_status = "0";
            sendInfo.updated_by = main.ConvertNullInt(strUserID);
            sendInfo.updated_when = DateTime.Now;

            send.Update(sendInfo);
        }

        protected string CheckIsForword()
        {
            string strForwordSendSeq = null;
            string strSendSeq = lblSendSeq.Text;

            M_MessageSend info = send.GetInfoFromFromSendSeq(strSendSeq);
            if (info != null)
            {
                strForwordSendSeq = info.send_seq.ToString();
            }

            return strForwordSendSeq;
        }

        protected void UpdateBorrowAllow(string strMoneyReqSeq, string strReplyType)
        {
            string strUserID = Session["EmpSeq"].ToString();
            M_FIM_MoneyRequests info = new M_FIM_MoneyRequests();

            if (strReplyType == "1")
                info.allow = "1";
            else
                info.allow = "0";

            info.money_req_seq = Convert.ToInt32(strMoneyReqSeq);
            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = DateTime.Today;

            mr.UpdateBorrowAllow(info);
        }

        protected void UpdateSendMessageStatus(string strSendSeq, string strMessageStatus)
        {
            string strUserID = Session["EmpSeq"].ToString();
            M_MessageSend info = new M_MessageSend();

            info.send_seq = Convert.ToInt32(strSendSeq);
            info.message_status = strMessageStatus;
            info.updated_by = main.ConvertNullInt(Session["EmpSeq"]);
            info.updated_when = DateTime.Now;

            send.UpdateMessageStatus(info);
        }

        protected void cmdEdit_Click(object sender, ImageClickEventArgs e)
        {
            cmdEditReply.Visible = true;
            rblReplyType.Enabled = true;
        }
    }
}